<?php

namespace App\Models\Payment;

use App\Models\Payment\Platform\ChinaUms;
use App\Models\Payment\Platform\EbankPay;
use App\Models\Payment\Platform\FuiouAggregatePay;
use App\Models\Payment\Platform\FuiouPay;
use App\Models\Payment\Platform\UnionPay;
use App\Traits\UuidTrait;
use Illuminate\Database\Eloquent\Model;

class PaymentChannel extends Model
{
    use UuidTrait;

    const TABLE = 'payment_channels';
    protected $table = self::TABLE;

    public $casts = [
        'disabled' => 'boolean',
        'parameters' => 'array'
    ];

    public $attributes = [
        'parameters' => '[]',
        'key_file' => '',
        'cert_file' => ''
    ];

    const TAG_WECHAT = 'WECHAT';
    const TAG_ALI = 'ALI';

    const CATEGORY_CHINAUMS = 'CHINAUMS';

    const CATEGORY_UNIONPAY_WECHATPAY = 'UNIONPAY-WECHATPAY';
    const CATEGORY_UNIONPAY_ALIPAY = 'UNIONPAY-ALIPAY';
    const CATEGORY_UNIONPAY_APP = 'UNIONPAY-APP';
    const CATEGORY_UNIONPAY_WECHATPAY_MINI = 'UNIONPAY-WECHATPAY-MINI';

    const CATEGORY_FUIOUPAY = 'FUIOUPAY';

    const CATEGORY_FUIOUPAY_AGGREGATE = 'FUIOUPAY-AGGREGATE';

    const CATEGORY_EBANKPAY = 'EBANKPAY';

    const CATEGORIES = [

        self::CATEGORY_CHINAUMS => '银联商务全民付',

        self::CATEGORY_UNIONPAY_WECHATPAY => '银联前置微信支付',
        self::CATEGORY_UNIONPAY_ALIPAY => '银联前置支付宝支付',
        self::CATEGORY_UNIONPAY_APP => '银联前置APP支付',
        self::CATEGORY_UNIONPAY_WECHATPAY_MINI => '银联前置小程序支付',
        self::CATEGORY_FUIOUPAY => '富友支付',
        self::CATEGORY_FUIOUPAY_AGGREGATE => '富友前置商户支付',
        self::CATEGORY_EBANKPAY => '城商行支付'
    ];

    const RULES = [
        self::CATEGORY_CHINAUMS => ChinaUms::RULE,
        self::CATEGORY_UNIONPAY_ALIPAY => UnionPay::RULE_V1,
        self::CATEGORY_UNIONPAY_APP => UnionPay::RULE_V2,
        self::CATEGORY_UNIONPAY_WECHATPAY => UnionPay::RULE,
        self::CATEGORY_UNIONPAY_WECHATPAY_MINI => UnionPay::RULE,
        self::CATEGORY_FUIOUPAY => FuiouPay::RULE,
        self::CATEGORY_FUIOUPAY_AGGREGATE => FuiouAggregatePay::RULE,
        self::CATEGORY_EBANKPAY => EbankPay::RULE
    ];

    const TAGS = [
        self::CATEGORY_UNIONPAY_WECHATPAY_MINI => [self::TAG_WECHAT],
        self::CATEGORY_CHINAUMS => [self::TAG_WECHAT, self::TAG_ALI],
        self::CATEGORY_UNIONPAY_ALIPAY => [self::TAG_ALI],
        self::CATEGORY_UNIONPAY_WECHATPAY => [self::TAG_WECHAT],
        self::CATEGORY_UNIONPAY_APP => [self::TAG_WECHAT],
        self::CATEGORY_FUIOUPAY => [self::TAG_WECHAT, self::TAG_ALI],
        self::CATEGORY_FUIOUPAY_AGGREGATE => [self::TAG_WECHAT, self::TAG_ALI],
        self::CATEGORY_EBANKPAY => [self::TAG_WECHAT, self::TAG_ALI],
    ];

    const TRADE_TYPES = [
        self::CATEGORY_CHINAUMS => [
            TradeType::WECHAT_JSAPI,
//            TradeType::WECHAT_JSAPI_MWEB,
            TradeType::WECHAT_MINI,
            TradeType::ALI_NATIVE, // 替代 TradeType::ALI_MWEB,
            TradeType::WECHAT_NATIVE //  替代 TradeType::WECHAT_MWEB,
        ],
        self::CATEGORY_UNIONPAY_APP => [
            TradeType::WECHAT_APP
        ],
        self::CATEGORY_UNIONPAY_WECHATPAY_MINI => [
            TradeType::WECHAT_MINI // = TradeType::WECHAT_JSAPI
        ],
        self::CATEGORY_UNIONPAY_ALIPAY => [
            TradeType::ALI_NATIVE
        ],
        self::CATEGORY_UNIONPAY_WECHATPAY => [
            TradeType::WECHAT_JSAPI,
            TradeType::WECHAT_NATIVE,
        ],
        self::CATEGORY_FUIOUPAY => [
            TradeType::ALI_JSAPI,
            TradeType::WECHAT_JSAPI,
            TradeType::WECHAT_MINI,
            TradeType::WECHAT_NATIVE,
            TradeType::ALI_NATIVE
        ],
        self::CATEGORY_FUIOUPAY_AGGREGATE => [
            TradeType::ALI_JSAPI,
            TradeType::WECHAT_JSAPI,
            TradeType::WECHAT_MINI,
            TradeType::WECHAT_NATIVE,
            TradeType::ALI_NATIVE
        ],
        self::CATEGORY_EBANKPAY => [
//            TradeType::WECHAT_JSAPI,
            TradeType::WECHAT_MINI,
            TradeType::WECHAT_NATIVE,
            TradeType::ALI_NATIVE
        ]
    ];

    public function getKeyFileAttribute($value)
    {
        if ($value) {
            $value = @decrypt($value) ?: '';
        }
        return $value;
    }

    public function getCertFileAttribute($value)
    {
        if ($value) {
            $value = @decrypt($value) ?: '';
        }
        return $value;
    }
}
